//
//  MoreCarInfoView.m
//  ICrEAM
//
//  Created by xphone on 17/1/6.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "MoreCarInfoView.h"
#import "MoreCarInfoTableViewCell.h"
#import "ICMCarModel.h"
#import "ICMPersonerModel.h"

@interface MoreCarInfoView ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation MoreCarInfoView

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        [self addSubview:self.tableView];
    }
    return self;
}

- (UITableView *)tableView
{
    if(!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerNib:[UINib nibWithNibName:@"MoreCarInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"MoreCarInfoTableViewCell"];
    }
    return _tableView;
}

- (void)resetViewFrame:(CGRect)rect
{
    self.frame = rect;
    self.tableView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.carArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MoreCarInfoTableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MoreCarInfoTableViewCell"];
    }
    
    ICMCarModel *carModel = [ICMCarModel yy_modelWithDictionary:self.carArray[indexPath.row]];
    ICMPersonerModel *personModel = [ICMPersonerModel yy_modelWithDictionary:self.personArray[indexPath.row]];
    
    if(indexPath.row == self.selectedIndex)
    {
        ((MoreCarInfoTableViewCell *)cell).isCellSelected = YES;
    }
    else
    {
        ((MoreCarInfoTableViewCell *)cell).isCellSelected = NO;
    }
    
    ((MoreCarInfoTableViewCell *)cell).license = carModel.carNum;
    ((MoreCarInfoTableViewCell *)cell).vin = carModel.vinNum;
    ((MoreCarInfoTableViewCell *)cell).name = personModel.ownerName;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ICMCarModel *carModel = [ICMCarModel yy_modelWithDictionary:self.carArray[indexPath.row]];
    if([self.delegate respondsToSelector:@selector(didClickedCellAtIndex:WithVIN:)])
    {
        [self.delegate didClickedCellAtIndex:indexPath.row WithVIN:carModel.vinNum];
    }
}

- (NSArray *)carArray
{
    if(!_carArray)
    {
        _carArray = [[NSArray alloc] init];
    }
    return _carArray;
}

- (NSArray *)personArray
{
    if(!_personArray)
    {
        _personArray = [[NSArray alloc] init];
    }
    return _personArray;
}

@end
